<template>
  <div class="backlogin">
    <div class="login_box">
      <div class="header">
        <img src="@/assets/login-bg.png" alt/>
      </div>
      <div class="title">用户登录</div>
      <div class="login-container">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on"
                 label-position="left"
        >
          <el-form-item prop="username">
            <el-input
                ref="username"
                v-model="loginForm.username"
                placeholder="Username"
                name="username"
                type="text"
                tabindex="1"
                auto-complete="on"
            />
          </el-form-item>

          <el-form-item prop="password">
            <el-input
                :key="passwordType"
                ref="password"
                v-model="loginForm.password"
                :type="passwordType"
                placeholder="Password"
                name="password"
                tabindex="2"
                auto-complete="on"
                @keyup.enter.native="handleLogin"
            />
            <span class="show-pwd" @click="showPwd">
                <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"/>
              </span>
          </el-form-item>

          <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;"
                     @click.native.prevent="handleLogin"
          >Login
          </el-button>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    // username: 'CJohnSnow' /*TODO:先预存测试值，以免手动输入*/,
    // password: '123456',
    // disablebtn: false,
    // loginText: '登录'

    const validateUsername = (rule, value, callback) => {
      value.length < 3
          ? callback(new Error('The password can not be less than 3 digits'))
          : callback()
    }
    const validatePassword = (rule, value, callback) => {
      value.length < 3
          ? callback(new Error('The password can not be less than 3 digits'))
          : callback()
    }
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{required: true, trigger: 'blur', validator: validateUsername}],
        password: [{required: true, trigger: 'blur', validator: validatePassword}]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined

    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    // login() {
    //   var _this = this
    //   this.disablebtn = true
    //   this.loginText = '登录中...'
    //   _this.$router.push({ path: '/Index' })
    // }

    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store
              .dispatch('user/login', this.loginForm)
              .then(() => {
                this.$router.push({path: this.redirect || '/'})
                this.$store.commit('header/SHOW_LOGIN_LINK', false)
                this.$store.commit('header/SHOW_REGISTER_LINK', false)
                this.$store.commit('header/SHOW_LOGOUT_LINK', true)
                this.loading = false
              })
              .catch(() => {
                this.loading = false
              })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }

  },
  created() {
    // 隐藏登录链接 显示注册链接
    this.$store.commit('header/SHOW_LOGIN_LINK', false)
    this.$store.commit('header/SHOW_REGISTER_LINK', true)
    this.$store.commit('header/SHOW_LOGOUT_LINK', false)
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="less" scoped>
.backlogin {
  height: 100%;
  background: url(../../assets/img/3.jpeg) no-repeat center/100%;
  overflow: hidden;
}

.header {
  height: 60px;
  margin-bottom: 10px;
}

.header img {
  width: 66px;
  height: 60px;
  border: 1px solid gray;
  border-radius: 50%;
  box-shadow: 0 1px 5px rgba(13, 62, 73, 0.2);
  padding: 2px;
}

.header img:hover {
  animation: ImgScale 1.2s ease;
}

@keyframes ImgScale {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.2);
  }
  100% {
    transform: scale(1);
  }
}

.header span {
  float: left;
  color: #566a80;
  margin: 21px 0 0 20px;
}

.box-content {
  padding: 12px;
  box-shadow: 0 1px 5px rgba(13, 62, 73, 0.2);
}

.login_box {
  width: 320px;
  margin: 50px auto;
  font-family: 'Helvetica Neue', Helvetica, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', '微软雅黑', Arial, sans-serif;
}

.login_box .myinput {
  width: 100%;
  border: 1px solid #cad3de;
  height: 40px;
  line-height: 40px;
  margin: 5px 0 10px;
  border-radius: 3px;
  padding: 0 10px;
  outline: none;
  box-sizing: border-box;
}

.login_box .myinput:focus {
  border: 1px solid #4289dc;
}

.login_other {
  overflow: hidden;
}

.login_other a {
  float: right;
  color: #727f8f;
}

.login_other a:hover {
  color: #273444;
}

.login_other input,
.login_other label {
  float: left;
  color: #727f8f;
}

.login_other input {
  margin: 4px 5px 0 0;
}

.login {
  box-sizing: border-box;
  border: none 0;
  height: 44px;
  line-height: 44px;
  width: 100%;
  background: #4187db;
  font-size: 16px;
  border-radius: 3px;
  margin-right: 40px;
  transition: all 0.5s ease;
  cursor: pointer;
  outline: none;
  color: #fff;
  margin-top: 15px;
}

.login:hover {
  background: #2668b5;
}

.login[disabled] {
  opacity: 0.8;
}

.login[disabled]:hover {
  background: #4187db;
}

.title {
  color: #273444;
  font-size: 1.5em;
  text-align: center;
  margin: 0 0 20px 0;
}

@media only screen and (max-width: 768px) {
  .login_box {
    width: 280px;
    margin: 50px auto;
  }
}

.svg-container {
  padding: 6px 5px 6px 15px;
  //color: $dark_gray;
  vertical-align: middle;
  width: 30px;
  display: inline-block;
}

.show-pwd {
  position: absolute;
  right: 10px;
  top: 7px;
  font-size: 16px;
  //color: $dark_gray;
  cursor: pointer;
  user-select: none;
}

</style>
